using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CentralOffice.Admin;

using MenuTable = CentralOffice.Admin.Data.Menus.MenusDataTable;
using MenuRow = CentralOffice.Admin.Data.Menus.MenusRow;
using Husb.Security;
using System.Text;

namespace CentralOffice.WebClient.ipanel
{
    public partial class menu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //GenericMembershipProvider membershipProvider = Membership.Provider as GenericMembershipProvider;
                //MembershipUserEx user = membershipProvider.GetUser(HttpContext.Current.User.Identity.Name, false) as MembershipUserEx;

                //string js = "jQuery.cookie('{0}', null);";
                //StringBuilder sb = new StringBuilder("<script type=\"text/javascript\">");
                //if (user.IsEmployee)
                //{
                //    if (user.EnterpriseId != null && user.EnterpriseId != Guid.Empty)
                //    {
                //        sb.Append(String.Format(js, SiteConstants.EmployeeId))
                //        .Append(String.Format(js, SiteConstants.Employee))
                //        .Append(String.Format(js, SiteConstants.DepartmentId))
                //        .Append(String.Format(js, SiteConstants.Department));

                //    }
                //    else
                //    {
                //        sb.Append(String.Format(js, SiteConstants.ContactId))
                //        .Append(String.Format(js, SiteConstants.Contact))
                //        .Append(String.Format(js, SiteConstants.EnterpriseId))
                //        .Append(String.Format(js, SiteConstants.Enterprise));
                //    }
                //}
                //else
                //{
                //    //sb.Append(String.Format(js, SiteConstants.EmployeeId))
                //    //    .Append(String.Format(js, SiteConstants.Employee))
                //    //    .Append(String.Format(js, SiteConstants.DepartmentId))
                //    //    .Append(String.Format(js, SiteConstants.Department));
                //}
                //sb.Append("</script>");

                
                //lblJavaScript.Text = sb.ToString();
            }
            
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string[] roles = HttpContext.Current.Items[SiteConstants.CurrentRoles] as string[];
            if (roles == null)
            {
                RolePrincipal rp = Page.User as RolePrincipal;
                if (rp == null)
                {
                    return;
                }
                roles = rp.GetRoles();
                HttpContext.Current.Items[SiteConstants.CurrentRoles] = roles;
            }

            RepeaterItem item = e.Item;
            MenuRow menuItem = (item.DataItem as DataRowView).Row as MenuRow;

            Repeater repeater = item.FindControl("Repeater2") as Repeater;

            MenuTable dv = CentralOffice.Admin.BusinessActions.Menu.GetSubMenu(menuItem.Id, roles);
            repeater.DataSource = dv;
            repeater.DataBind();
        }

        protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string[] roles = HttpContext.Current.Items[SiteConstants.CurrentRoles] as string[];
            if (roles == null)
            {
                RolePrincipal rp = Page.User as RolePrincipal;
                if (rp == null)
                {
                    return;
                }
                roles = rp.GetRoles();
                HttpContext.Current.Items[SiteConstants.CurrentRoles] = roles;
            }

            RepeaterItem item = e.Item;

            if (item.ItemType != ListItemType.Footer)
            {
                MenuRow menuItem = (item.DataItem as DataRowView).Row as MenuRow;
                Literal lbl = item.FindControl("Literal1") as Literal;
                MenuTable dv = CentralOffice.Admin.BusinessActions.Menu.GetSubMenu(menuItem.Id, roles);
                if (dv.Rows.Count > 0)
                {
                    lbl.Text = String.Format(@"<li class=""L21""><a href=""javascript:c('f{0}');"" id=""f{0}""><span><img src=""{1}"" align=""absMiddle""/>{2}</span></a></li>", menuItem.MenuCode.Substring(1), menuItem.ImageURL, menuItem.Name);

                    //<ul id='f<%# Eval("MenuCode") %>d' style="display:none;">
                    Literal lblLevel3Start = item.FindControl("lblLevel3Start") as Literal;
                    if (lblLevel3Start != null) lblLevel3Start.Text = "<ul id='f" + menuItem.MenuCode.Substring(1) + "d' style=\"display:none;\">";
                    Literal lblLevel3End = item.FindControl("lblLevel3End") as Literal;
                    if (lblLevel3End != null) lblLevel3End.Text = "</ul>";

                    Repeater repeater = item.FindControl("Repeater3") as Repeater;
                    repeater.DataSource = CentralOffice.Admin.BusinessActions.Menu.GetSubMenu(menuItem.Id, roles);
                    repeater.DataBind();
                }
                else
                {
                    lbl.Text = String.Format(@"<li class=""L22""><a href=""javascript:a('{0}','{1}', 0);"" id=""f{1}""><span><img src=""{2}"" align=""absMiddle""/> {3}</span></a></li>", menuItem.RelativeURL, menuItem.MenuCode.Substring(1), menuItem.ImageURL, menuItem.Name);
                }
            }
        }

        protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            string[] roles = HttpContext.Current.Items[SiteConstants.CurrentRoles] as string[];
            if (roles == null)
            {
                RolePrincipal rp = Page.User as RolePrincipal;
                if (rp == null)
                {
                    e.InputParameters["roles"] = "";
                    return;
                }
                roles = rp.GetRoles();
                HttpContext.Current.Items[SiteConstants.CurrentRoles] = roles;
            }
            e.InputParameters["roles"] = roles;
        }
    }
}
